package com.tc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tc.domain.Drugs_info;
import com.tc.dto.Drugs_infoQueryDTO;
import com.tc.mapper.Drugs_infoMapper;
import com.tc.service.IDrugs_infoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tc.vo.Drugs_infoVO;
import com.tc.vo.PageResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@Service
public class Drugs_infoServiceImpl extends ServiceImpl<Drugs_infoMapper, Drugs_info> implements IDrugs_infoService {

    /**
     * 分页查询药品信息
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @Override
    public PageResultVO<Drugs_infoVO> pageQuery(Drugs_infoQueryDTO queryDTO) {
        // 构建分页查询对象
        Page<Drugs_info> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        
        // 构建查询条件
        LambdaQueryWrapper<Drugs_info> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.hasText(queryDTO.getClassification())) {
            queryWrapper.eq(Drugs_info::getClassification, queryDTO.getClassification());
        }
        if (StringUtils.hasText(queryDTO.getDrugName())) {
            queryWrapper.like(Drugs_info::getDrugName, queryDTO.getDrugName());
        }
        if (StringUtils.hasText(queryDTO.getTradeName())) {
            queryWrapper.like(Drugs_info::getTradeName, queryDTO.getTradeName());
        }
        if (StringUtils.hasText(queryDTO.getManufacturer())) {
            queryWrapper.like(Drugs_info::getManufacturer, queryDTO.getManufacturer());
        }
        
        // 执行分页查询
        IPage<Drugs_info> pageResult = this.page(page, queryWrapper);
        
        // 转换records
        List<Drugs_infoVO> voList = new ArrayList<>();
        for (Drugs_info drugsInfo : pageResult.getRecords()) {
            Drugs_infoVO vo = new Drugs_infoVO();
            BeanUtils.copyProperties(drugsInfo, vo);
            voList.add(vo);
        }
        
        // 使用通用PageResultVO
        return PageResultVO.convert(pageResult, voList);
    }
}
